﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OneTwoTeam.EasyScrum.ASPMVC.Models;

namespace OneTwoTeam.EasyScrum.ASPMVC.Controllers
{
    public class SprintController : Controller
    {
        private easyscrumEntities db = new easyscrumEntities();

        // GET: /Sprint/
        public ActionResult Index()
        {
            var sprint = db.sprint.Include(s => s.project).Include(s => s.sprint_backlog);
            return View(sprint.ToList());
        }

        // GET: /Sprint/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            sprint sprint = db.sprint.Find(id);
            if (sprint == null)
            {
                return HttpNotFound();
            }
            return View(sprint);
        }

        // GET: /Sprint/Create
        public ActionResult Create()
        {
            ViewBag.id_project = new SelectList(db.project, "id", "nom");
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom");
            return View();
        }

        // POST: /Sprint/Create
        // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
        // plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="id,nom,date_debut,date_fin,id_project,id_sprint_backlog")] sprint sprint)
        {
            if (ModelState.IsValid)
            {
                db.sprint.Add(sprint);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.id_project = new SelectList(db.project, "id", "nom", sprint.id_project);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", sprint.id_sprint_backlog);
            return View(sprint);
        }

        // GET: /Sprint/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            sprint sprint = db.sprint.Find(id);
            if (sprint == null)
            {
                return HttpNotFound();
            }
            ViewBag.id_project = new SelectList(db.project, "id", "nom", sprint.id_project);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", sprint.id_sprint_backlog);
            return View(sprint);
        }

        // POST: /Sprint/Edit/5
        // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
        // plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="id,nom,date_debut,date_fin,id_project,id_sprint_backlog")] sprint sprint)
        {
            if (ModelState.IsValid)
            {
                db.Entry(sprint).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_project = new SelectList(db.project, "id", "nom", sprint.id_project);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", sprint.id_sprint_backlog);
            return View(sprint);
        }

        // GET: /Sprint/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            sprint sprint = db.sprint.Find(id);
            if (sprint == null)
            {
                return HttpNotFound();
            }
            return View(sprint);
        }

        // POST: /Sprint/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            sprint sprint = db.sprint.Find(id);
            db.sprint.Remove(sprint);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
